// page-deviceImg/home.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    url: app.globalData.requestURL +'upload/uploadImgTest',
    path: '',//文件本地路径
    name: '',//name文件名
    imagesType: '',//自定义上传的目录路径
    zhengqianfang: wx.getStorageSync('zhengqianfang'),
    zuoqianfang: wx.getStorageSync('zuoqianfang'),
    youqianfang: wx.getStorageSync('youqianfang'),
    youhoufang: wx.getStorageSync('youhoufang'),
    zuohoufang: wx.getStorageSync('zuohoufang'),
    bg_zhengqianfang:'',
    bg_zuoqianfang: '',
    bg_youqianfang: '',
    bg_youhoufang: '',
    bg_zuohoufang: '',
    bigShelfNum: '',
    qylxr: false,
    isSubmit: true
  },
  getBigShelfNum: function (e) {
    this.setData({
      bigShelfNum: e.detail.value
    });
  },
  uploadimg: function (e) {
    app.uploadimg(e);
  },
  chooseImage: function (e) {
    var self = this;
    wx.chooseImage({
      count: 1,
      sourceType: ['camera'],
      success: function (res) {
        if (e.currentTarget.id == '0') {
          self.setData({
            name: "zhengqianfang",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.bigShelfNum + '/' + "waiguan"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('zhengqianfang', imgResponseUrl)
            self.setData({
              zhengqianfang: wx.getStorageSync('zhengqianfang')
            })
            if (self.data.zhengqianfang != ''){
              self.setData({
                bg_zhengqianfang: ''
              })
            }
          };
        } else if (e.currentTarget.id == '1') {
          self.setData({
            name: "zuoqianfang",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.bigShelfNum + '/' + "waiguan"
          });    
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('zuoqianfang', imgResponseUrl)
            self.setData({
              zuoqianfang: wx.getStorageSync('zuoqianfang')
            })
            if (self.data.zuoqianfang != '') {
              self.setData({
                bg_zuoqianfang: ''
              })
            }
          };
        } else if (e.currentTarget.id == '2') {
          self.setData({
            name: "youqianfang",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.bigShelfNum + '/' + "waiguan"
          })
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('youqianfang', imgResponseUrl)
            self.setData({
              youqianfang: wx.getStorageSync('youqianfang')
            })
            if (self.data.youqianfang != '') {
              self.setData({
                bg_youqianfang: ''
              })
            }
          };
        } else if (e.currentTarget.id == '3') {
          self.setData({
            name: "youhoufang",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.bigShelfNum + '/' + "waiguan"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('youhoufang', imgResponseUrl)
            self.setData({
              youhoufang: wx.getStorageSync('youhoufang')
            })
            if (self.data.youhoufang != '') {
              self.setData({
                bg_youhoufang: ''
              })
            }
          };
        } else if (e.currentTarget.id == '4') {
          self.setData({
            name: "zuohoufang",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.bigShelfNum + '/' + "waiguan"
          })
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('zuohoufang', imgResponseUrl)
            self.setData({
              zuohoufang: wx.getStorageSync('zuohoufang')
            })
            if (self.data.zuohoufang != '') {
              self.setData({
                bg_zuohoufang: ''
              })
            }
          };
        }
      }
    });
  },
  submit: function (e) {
    var that = this;
    if (that.data.isSubmit) {
      that.setData({
        isSubmit: false
      })
      var bigShelfNum = that.data.bigShelfNum;//设备编号
      var deviceFrontImg = that.data.zhengqianfang; //设备正前方
      if (deviceFrontImg == "" || deviceFrontImg == null) {
        wx.showToast({
          title: '照片错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        return false;
      }
      var deviceFrontRightImg = that.data.youqianfang;
      if (deviceFrontRightImg == "" || deviceFrontRightImg == null) {
        wx.showToast({
          title: '照片错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        return false;
      }
      var deviceFrontLiftImg = that.data.zuoqianfang;
      if (deviceFrontLiftImg == "" || deviceFrontLiftImg == null) {
        wx.showToast({
          title: '照片错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        return false;
      }
      var deviceLiftAfterImg = that.data.zuohoufang;
      if (deviceLiftAfterImg == "" || deviceLiftAfterImg == null) {
        wx.showToast({
          title: '照片错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        return false;
      }
      var deviceRightAfterImg = that.data.youhoufang;
      if (deviceRightAfterImg == "" || deviceRightAfterImg == null) {
        wx.showToast({
          title: '照片错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        return false;
      }
      wx.request({
        url: app.globalData.requestURL +'/deviceImg/save',
        method: 'POST',
        data: {
          bigShelfNum: bigShelfNum, //设备编号
          deviceFrontImg: deviceFrontImg, //设备正前方
          deviceFrontRightImg: deviceFrontRightImg, //设备右前方
          deviceFrontLiftImg: deviceFrontLiftImg,//设备左前方
          deviceLiftAfterImg: deviceLiftAfterImg,//设备左后方
          deviceRightAfterImg: deviceRightAfterImg//设备右后方
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res.data)
          if (res.data.status == '0') {
            wx.setStorageSync('zhengqianfang', '');
            wx.setStorageSync('zuoqianfang', '');
            wx.setStorageSync('youqianfang', '');
            wx.setStorageSync('youhoufang', '');
            wx.setStorageSync('zuohoufang', '');
            wx.showToast({
              title: '设备添加成功',
              icon: 'success',
              duration: 2000,
              mask: true,
              success: function () {
                wx.redirectTo({
                  url: '/page-billOfDocumentImg/bodImg-home'
                })
              }
            });
          } else {
            wx.showToast({
              title: '设备添加失败',
              icon: 'loading',
              duration: 2000,
              mask: true,
              success: function () {
                that.setData({
                  isSubmit: true
                })
              }
            });
          }
        }
      })
    } else {
      wx.showToast({
        title: '不可重复提交',
        icon: 'loading',
        duration: 2000,
        mask: true
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      bigShelfNum: wx.getStorageSync("factoryNum")
    })
    var bigShelfNum = that.data.bigShelfNum
    if (wx.getStorageSync('zhengqianfang') == null || wx.getStorageSync('zhengqianfang') == '') {
      that.setData({
        bg_zhengqianfang: app.globalData.registerImage[8]
      })
    }
    if (wx.getStorageSync('zuoqianfang') == null || wx.getStorageSync('zuoqianfang') == '') {
      that.setData({
        bg_zuoqianfang: app.globalData.registerImage[9]
      })
    }
    if (wx.getStorageSync('youqianfang') == null || wx.getStorageSync('youqianfang') == '') {
      that.setData({
        bg_youqianfang: app.globalData.registerImage[10]
      })
    }
    if (wx.getStorageSync('youhoufang') == null || wx.getStorageSync('youhoufang') == '') {
      that.setData({
        bg_youhoufang: app.globalData.registerImage[11]
      })
    }
    if (wx.getStorageSync('zuohoufang') == null || wx.getStorageSync('zuohoufang') == '') {
      that.setData({
        bg_zuohoufang: app.globalData.registerImage[12]
      })
    }

    //查询此照片表是否有提交过照片
    if (that.data.bigShelfNum != '' && that.data.bigShelfNum != null){
    wx.request({
      url: app.globalData.requestURL+'deviceImg/getDeviceImgByCondition',
      method: 'POST',
      data: {
        bigShelfNum: bigShelfNum
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //如果有
        if (res.data.status == '1') {
          wx.redirectTo({
            url: '/page-billOfDocumentImg/bodImg-home'
          })
        }
      }
    })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})